#include "PreviewItem.h"
#include "ui_PreviewItem.h"

#include <QMouseEvent>
#include <QDebug>
#include <QStyle>
PreviewItem::PreviewItem(QWidget *parent)
    : QPushButton(parent)
    , ui(new Ui::PreviewItem)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_StyledBackground, true);
    // 连接信号槽
    connect(this, &PreviewItem::toggled, this, &PreviewItem::setCheckedChange);
    setCheckable(true);
}
PreviewItem::~PreviewItem()
{
    delete ui;
}

void PreviewItem::setImage(const QImage &image)
{
    if (!image.isNull()) {
        // 缩放图像以适应标签大小
        QPixmap pixmap = QPixmap::fromImage(
            image.scaled(ui->imageLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation)
        );
        ui->imageLabel->setPixmap(pixmap);
    } else {
        ui->imageLabel->clear();
    }
}

void PreviewItem::setImageGuid(const QString &imageGuid)
{
    m_imageGuid = imageGuid;
}

QString PreviewItem::getImageGuid() const
{
    return m_imageGuid;
}

void PreviewItem::on_closeButton_clicked()
{
    // 确保GUID不为空再发出删除请求信号
    if (!m_imageGuid.isEmpty()) {
        emit deleteRequested(m_imageGuid);
    }
}

void PreviewItem::setCheckedChange(bool checked)
{
    if(checked)
    {
        emit selectImageRequested(m_imageGuid);
    }
    // 设置动态属性
    setProperty("Checked", checked);
    // 更新样式
    style()->unpolish(this);
    style()->polish(this);
    update();
}
